<template>
  <div class="container">
    <div class="main">
      <div class="form-box">
        <div class="form-item-box">
          <div class="title">
            <div class="text">您好！</div>
            <div class="desc">欢迎登录{{ appTitle }}</div>
          </div>
          <div class="form">
            <!-- @submit.native.prevent 防止表单提交刷新页面 -->
            <el-form :model="loginForm" :rules="loginRules" ref="loginFormRef" @submit.native.prevent>
              <el-form-item prop="username">
                <el-input v-model.trim="loginForm.username" placeholder="请输入用户名" clearable>
                  <template #prefix>
                    <i class="iconfont icon-yonghu"></i>
                  </template>
                </el-input>
              </el-form-item>
              <el-form-item prop="password">
                <el-input v-model.trim="loginForm.password" placeholder="请输入登录密码" type="password" show-password clearable>
                  <template #prefix>
                    <i class="iconfont icon-mima"></i>
                  </template>
                </el-input>
              </el-form-item>
              <el-form-item>
                <!-- 在表单的提交按钮上添加 Vue 原生属性 native-type="submit" 可以让按钮变为表单提交按钮 -->
                <el-button class="form-button" type="primary" native-type="submit" @click="onLogin" :loading="loginLoading">登录</el-button>
              </el-form-item>
            </el-form>
          </div>
        </div>
      </div>
      <div class="info-box">
        <div class="info">
          <div class="title">{{ appTitle }}</div>
          <div class="desc">网站制作者：ydl</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { login } from '@/api/login.js'
import { setAuth } from '@/utils/auth.js'
import { ElMessage } from 'element-plus'

import { ref } from 'vue'

import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
const router = useRouter()
const store = useStore()

// 网站名称
const appTitle = import.meta.env.VITE_APP_TITLE

const loginFormRef = ref()
const loginLoading = ref(false)
const loginForm = ref({
  username: '', // 用户名
  password: '', // 密码
  loginType: 0, // 登录类型 0：后台，1：前台
})
const loginRules = ref({
  username: [{ required: true, message: '请输入用户名称', trigger: 'blur' }],
  password: [{ required: true, message: '请输入登录密码', trigger: 'blur' }],
})
function onLogin() {
  loginFormRef.value.validate((valid) => {
    if (valid) {
      loginLoading.value = true
      login(loginForm.value).then(res => {
        if (res && res.code === 200) {
          sessionStorage.setItem('authorization', res.data)
          setTimeout(() => {
            router.replace({ path: '/home' })
            ElMessage({
              message: res.message,
              grouping: true,
              type: 'success'
            })
            loginLoading.value = false
          }, 800)
        } else {
          ElMessage({
            message: res.message,
            grouping: true,
            type: 'error',
          })
          loginLoading.value = false
        }
      }).catch(() => {
        loginLoading.value = false
      })
    }
  })
}

</script>

<style lang="scss" scoped>
.container {
  width: 100vw;
  height: 100vh;
  background-image: url('@/assets/images/background_login.jpg');
  background-repeat: no-repeat;
  background-size: 100% 100%;
  background-attachment: fixed; // 背景图片固定
  position: relative;

  display: flex;
  justify-content: center;
  align-items: center;
  .main {
    width: 45%;
    height: 60%;
    background-color: rgba($color: #fff, $alpha: 0.4);
    border-radius: 20px;
    -webkit-box-shadow: 2px 2px 5px 0px rgba(50, 50, 50, 0.29);
    -moz-box-shadow: 2px 2px 5px 0px rgba(50, 50, 50, 0.29);
    box-shadow: 2px 2px 5px 0px rgba(50, 50, 50, 0.29);
    display: flex;
    justify-content: space-between;
    animation: up 1s;

    .form-box {
      width: 60%;
      height: 100%;
      background-color: #fff;
      display: flex;
      justify-content: center;
      align-items: center;
      position: relative;
      border-radius: 20px 0px 0px 20px;

      .form-item-box {
        width: 80%;
        height: 80%;
        .title {
          .text {
            letter-spacing: 5px;
            font-size: 26px;
          }
          .desc {
            color: #333;
            letter-spacing: 3px;
            font-size: 16px;
          }
        }
        .form {
          width: 100%;
          margin-top: 50px;
          .form-button {
            width: 100%;
            margin-top: 20px;
            letter-spacing: 3px;
          }
        }
      }
    }

    .info-box {
      width: 40%;
      height: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
      border-radius: 0px 20px 20px 0px;

      .info {
        height: 80%;
        width: 80%;
        display: flex;
        flex-direction: column;
        justify-content: space-between;

        .title {
          font-size: 28px;
          color: #fff;
        }
        .desc {
          font-size: 16px;
          color: #fff;
        }
      }
    }
  }
}
</style>